/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.Material;
import com.click.app.model.MeasureUnit;
import com.click.app.service.GenericManager;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author orgale
 */
public class MeasureUnitAction extends BaseAction {

    private GenericManager<MeasureUnit, Long> measureUnitManager;
    private GenericManager<Material, Long> materialManager;
    private List measureUnits;
    private MeasureUnit measureUnit;
    private Long id;

    public void setId(Long id) {
        this.id = id;
    }

    public MeasureUnit getMeasureUnit() {
        return measureUnit;
    }

    public void setMeasureUnit(MeasureUnit measureUnit) {
        this.measureUnit = measureUnit;
    }

    public boolean materialsFree() {
        boolean resp = true;

        List<Material> mats = materialManager.getAll();
        for (Material m : mats) {
            if (m.getUnitMeasure().equals(measureUnit)) {
                resp = false;
            }
        }

        return resp;
    }

    public String delete() {
        if (materialsFree()) {
            measureUnitManager.remove(new Long(measureUnit.getId()));
            saveMessage(getText("measureUnit.deleted"));
        } else {
            List messages = (List) getRequest().getSession().getAttribute("errors");
            if (messages == null) {
                messages = new ArrayList();
            }
            messages.add(getText("measureUnit.deleteError"));
            getRequest().getSession().setAttribute("errors", messages);

        }
        return SUCCESS;

    }

    public String edit() {
        if (id != null) {
            measureUnit = measureUnitManager.get(id);
        } else {
            measureUnit = new MeasureUnit();
        }

        return SUCCESS;
    }

    public boolean validationPasses() {
        boolean vp = true;
        if (measureUnit.getName() == null || measureUnit.getName().matches("")) {
            errorMessage("Campo requerido, nombre.");
            vp = false;
        }
        if (measureUnit.getDescription() == null || measureUnit.getDescription().matches("")) {
            errorMessage("Campo requerido, descripción.");
            vp = false;
        }
       
        return vp;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return CANCEL;
        }

        if (delete != null) {
            return delete();
        }
        System.out.println("Saving measure unit...");
        boolean isNew = false;
        if (measureUnit.getId() == null) {
            isNew = true;
        }

        if (validationPasses()){
            measureUnit = measureUnitManager.save(measureUnit);
        }else{
            return ERROR;
        }
        

        String key = (isNew) ? "measureUnit.added" : "measureUnit.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    public void setMeasureUnitManager(GenericManager<MeasureUnit, Long> measureUnitManager) {
        this.measureUnitManager = measureUnitManager;
    }

    public void setMaterialManager(GenericManager<Material, Long> materialManager) {
        this.materialManager = materialManager;
    }

    public List getMeasureUnits() {
        return measureUnits;
    }

    public String list() {
        measureUnits = measureUnitManager.getAll();
        return SUCCESS;
    }
    private List fruits;

    @Override
    public String execute() throws Exception {
        fruits = new ArrayList();
        fruits.add("Apple");
        fruits.add("Mango");
        fruits.add("Orange");
        fruits.add("Pine Apple");
        return SUCCESS;

    }

    public List getFruits() {
        return fruits;
    }

    public String helpList(){
        return SUCCESS;
    }

    public String help(){
        return SUCCESS;
    }

}